/**
 * @file Callback.c
 * @author 闂傚倸瀚▔娑㈡偤濞嗘劖缍囬柨鐕傛嫹 (2405973406@qq.com)
 * @brief hal闁圭厧鐡ㄩ幐鐑芥嚈閹达箑妫樻い鎾跺仦缁€鈧柣鐘差儏閸燁偊宕甸柆宥呮瀬闁规鍠楅悾閬嶆倵閸︻厼浠ф鐑囨嫹
 * @version 1.0
 * @date 2023-07-12
 *
 * @copyright Copyright (c) 2023
 *
 */

/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "main.h"
#include "tim.h"
#include "imu.h"
#include "string.h"
#include "AS5047.h"
#include "usart.h"
#define rx_buf_size 256
#define M_PI 3.1415926535

#define hand GPIOB
#define arm GPIOC
#define leg GPIOF
//#define lock GPIOF

#define hand_PIN GPIO_PIN_0///爪子
#define arm_PIN GPIO_PIN_4///推臂
#define leg_PIN GPIO_PIN_8//运球脚

/*************码盘相关代码*****************/
/*
日期:2025-3-26
杨逸翔
*/
extern uint8_t AS5047_rx_buff;//接收帧
extern AS5047_raw_t AS_5047;
int  cnt = 0;//计算帧率
/******************************************/

/*************A板数据接收******************/
uint8_t index_temp_1 = 0;
extern uint8_t rx_state_uart4;
extern uint8_t rx_ch_uart4;
extern uint8_t index_temp;


void Rx_analyze_uart4(void);
/******************************************/

extern uint8_t index;/*投篮点位的下角标*/


uint32_t number = 0;
uint32_t id = 0;
extern uint8_t get_count;             /*光电门检测的次数*/
float convert_angle(float degrees) {
    // 将角度转换为弧度
    float radians = degrees * ((float)M_PI / 180.0f);
    // 调整弧度范围到 -π 到 π
    if (radians > (float)M_PI) {
        radians -= 2 * (float)M_PI;
    }
    else if (radians < -(float)M_PI) {
        radians += 2 * (float)M_PI;
    }
    return radians;
}
/**
 * @brief
 *
 * @param hcan
 */

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    CAN_RxHeaderTypeDef rx_header;
    uint8_t rx_data[8];
    HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rx_header, rx_data);
    switch (rx_header.StdId)
    {
    case 0x201:
    case 0x202:
    case 0x203:
    case 0x204:
//        M3508_Analyze(rx_header.StdId, rx_data);
//        break;
    case 0x205:
    case 0x206:
    case 0x207:
    case 0x208:
//        GM6020_Analyze(rx_header.StdId, rx_data);
      M3508_Analyze(rx_header.StdId, rx_data);
     break;
    
    }
    
}

void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    CAN_RxHeaderTypeDef rx_header;
    uint8_t rx_data[8];
    HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &rx_header, rx_data);
    switch (rx_header.StdId)
    {
    case 0x201:
    case 0x202:
    case 0x203:
    case 0x204:
//        M3508_Analyze(rx_header.StdId, rx_data);
//        break;
    case 0x205:
    case 0x206:
    case 0x207:
    case 0x208:
//        GM6020_Analyze(rx_header.StdId, rx_data);
      M3508_Analyze(rx_header.StdId, rx_data);
     break;
    
    }
    
}

void Rx_analyze_uart4(void)
{
    switch(rx_state_uart4)
    {
        case 0:/*等待接收帧头0xAA*/
          if(rx_ch_uart4 == 0x20)
          {
             rx_state_uart4 = 1;
          }
          else{rx_state_uart4 = 0;}
        break;
        
        case 1:
          index_temp_1 = rx_ch_uart4;
          rx_state_uart4 = 2;
        break;
        
        case 2:/*等待接收帧尾0xBB*/
          if(rx_ch_uart4 == 0xBB)
          {
            index_temp = index_temp_1;
          }
          rx_state_uart4 = 0;
        break;
    }

}


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    /***********手柄数据接收************/
    if(huart->Instance == UART7)
    {
        SBUS_Analyze();  
        HAL_UART_Receive_IT(&huart7, &SBUS_Receive_ch, 1); // 接收到一帧数据后再次开启空闲中断
    }
    /***********码盘数据接收************/
    if(huart->Instance == USART3)
    {
        uint8_t ch;
        ch = AS5047_rx_buff;
        cnt++;
        HAL_UART_Receive_IT(&huart3,&AS5047_rx_buff,1);
        int flag = check(&AS_5047,ch);/*flag = 1,数据帧发送成功,否则失败*/
        
        Robot.Robot_in_self.position_now.x = AS_5047.AS5047_data.X_W_t/100;
        Robot.Robot_in_self.position_now.y = AS_5047.AS5047_data.Y_W_t/100;
        Robot.Robot_in_self.position_now.yaw = convert_angle(AS_5047.AS5047_data.YAW);
    }
    /***********A板数据接收*************/
    if(huart->Instance == UART4)
    {
            Rx_analyze_uart4();
            HAL_UART_Receive_IT(&huart4,&rx_ch_uart4,1);
    }
}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == photogate_Pin)
  {
    
    if(claw_flag == 1){//Confirm the need to dribble and then pinch the ball

          while(__HAL_TIM_GET_COUNTER(&htim5) < (120000*1.5)){/*120000*1.5，延时处理光电门的关断*/}
          HAL_GPIO_WritePin (hand,hand_PIN,GPIO_PIN_RESET);
          HAL_TIM_Base_Stop(&htim5);
          __HAL_TIM_SET_COUNTER(&htim5,0);
          claw_flag = 0;
    }
    
  }
}